package com.example.controller;


import com.example.model.auto.Dynamic;
import com.example.model.auto.News;
import com.example.service.IDynamicService;
import com.example.service.INewsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 达瓦里氏
 * @since 2021-12-07
 */
@RestController
@RequestMapping("/news")
@CrossOrigin
public class NewsController {

    @Resource
    private INewsService iNewsService;

    /*查询课程*/
    @GetMapping("/getNews")
    public List<News> getNews(){
        return iNewsService.list();
    }

    /*通过id查询课程*/
    @GetMapping("/getNewsById/{id}")
    public News getNewsById(@PathVariable("id") int id){
        return iNewsService.getById(id);
    }

    /*修改课程*/
    @PostMapping("/updateNews")
    public void updateNews(@RequestBody News news){
        iNewsService.updateById(news);
    }

    /*添加课程*/
    @PostMapping("/setNews")
    public void setNews(@RequestBody News news){ iNewsService.save(news); }

    /*删除课程*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id){ return iNewsService.removeById(id); }


    /*图片上传*/
    @PostMapping("/elupload")
    public String elUpload(MultipartFile file){
        String fileName = file.getOriginalFilename();
        File dest = new File("D:\\project\\study\\vue\\static\\News_img\\" + fileName);
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败！";
    }

}
